package com.sign2024.sign2024project.capitalconstruction.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.param.PageParam;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.capitalconstruction.JjHistoryData;
import com.sign2024.sign2024project.capitalconstruction.mapper.JjHistoryDataMapper;
import com.sign2024.sign2024project.capitalconstruction.service.JjHistoryDataService;
import com.sign2024.sign2024project.goodsmaterials.vo.WzHistoryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yujie
 * @data 2024/11/18 8:19
 */
@Service
public class JjHistoryDataServiceImpl implements JjHistoryDataService {

    @Resource
    private JjHistoryDataMapper jjHistoryDataMapper;


    @Override
    public void exportData(List<JjHistoryData> list) {
        if (!list.isEmpty()) {
            if (StringUtils.isNull(list.get(0).getSkillClass())) {
                throw new CommonException(CommonConstants.ERROR, "请检查excel是否正确");
            }
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isNotNull(list.get(i))) {
                    if (StringUtils.isNotNull(list.get(i).getSectionName())) {
                        // 手动处理 合并单元格后的数据
                        if (StringUtils.isNull(list.get(i).getSkillClass())) {
                            // 使用列表上一个数据
                            if (i != 0) {
                                String skillClass = list.get(i - 1).getSkillClass();
                                list.get(i).setSkillClass(skillClass);
                                list.set(i, list.get(i));
                            }
                        }
                    }
                }
            }
        }

        for (int i = 0; i < list.size(); i++) {
            String skillClass = list.get(i).getSkillClass();
            // 去掉“组”字
            String numberStr = skillClass.replace("组", "");
            // 将中文数字转化为阿拉伯数字
            Integer number = chineseToArabic(numberStr);
            list.get(i).setSkillNum(Integer.valueOf(numberStr));
//            list.get(i).setSkillClass(number + "组");
            list.set(i, list.get(i));
            jjHistoryDataMapper.insertJjHistoryData(list.get(i));
        }
    }

    @Override
    public Page<JjHistoryData> pageJjHistoryData(PageParam param) {
        Integer offset = (param.getPageNum() - 1) * param.getPageSize();
        param.setOffset(offset);

        // 分页查询
        Page<JjHistoryData> page = new Page<>(param.getPageNum(), param.getPageSize());
        List<JjHistoryData> list = jjHistoryDataMapper.selectJjHistoryDataByPage(param);

        // 页码
        page.setCurrent(param.getPageNum());
        page.setSize(param.getPageSize());

        // 总记录数
        Long aLong = jjHistoryDataMapper.pageJjHistoryDataCount(param);
        page.setTotal(aLong);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<WzHistoryVo> selectJjHistoryVo(String sectionName, Integer skillNum) {
        return jjHistoryDataMapper.selectJjHistoryVo(sectionName, skillNum);
    }

    @Override
    public void importJjHistory(List<JjHistoryData> list) {
        if (!list.isEmpty()) {
            if (StringUtils.isNull(list.get(0).getSkillClass())) {
                throw new CommonException(CommonConstants.ERROR, "请检查excel是否正确");
            }
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isNotNull(list.get(i))) {
//                    if (StringUtils.isNotNull(list.get(i).getSectionName())) {
                    // 手动处理 合并单元格后的数据
                    if (StringUtils.isNull(list.get(i).getSkillClass())) {
                        // 使用列表上一个数据
                        if (i != 0) {
                            String skillClass = list.get(i - 1).getSkillClass();
                            list.get(i).setSkillClass(skillClass);
                            list.set(i, list.get(i));
                        }
//                        }
                    }
                }
            }
        }

        // 按照 组 将数据 封装为 map
        Map<String, List<JjHistoryData>> groupMap = list.stream()
                .collect(Collectors.groupingBy(JjHistoryData::getSkillClass));
        for (Map.Entry<String, List<JjHistoryData>> entry : groupMap.entrySet()) {
            // 获取 最大 组 数值
            Integer maxSkill = jjHistoryDataMapper.selectMaxSkill();
            if (maxSkill == null) {
                maxSkill = 0;
            }
            List<JjHistoryData> dataList = entry.getValue();
            int flag = maxSkill + 1;
            if (!dataList.isEmpty()) {
                for (JjHistoryData data : dataList) {
                    if (data == null) {
                        continue;
                    }
                    if (data.getSectionName() == null) {
                        continue;
                    }
                    data.setSkillNum(flag);
                    data.setSkillClass(flag + "组");
                    jjHistoryDataMapper.insertJjHistoryData(data);
                }
            }
        }
    }

    public static int chineseToArabic(String chineseNumber) {
        int result = 0;
        int temp = 0;
        for (char c : chineseNumber.toCharArray()) {
            if (chineseToArabicMap.containsKey(c)) {
                temp = chineseToArabicMap.get(c);
                result = result * 10 + temp;
            }
        }
        return result;
    }

    private static final Map<Character, Integer> chineseToArabicMap = new HashMap<>();

    static {
        chineseToArabicMap.put('零', 0);
        chineseToArabicMap.put('一', 1);
        chineseToArabicMap.put('二', 2);
        chineseToArabicMap.put('三', 3);
        chineseToArabicMap.put('四', 4);
        chineseToArabicMap.put('五', 5);
        chineseToArabicMap.put('六', 6);
        chineseToArabicMap.put('七', 7);
        chineseToArabicMap.put('八', 8);
        chineseToArabicMap.put('九', 9);
    }
}
